// 用户信息、能否编辑、身份验证状态
const userInfoObj = () => {
  return {
    userInfo: JSON.parse(sessionStorage.getItem('userInfo')) || {},
    userId: sessionStorage.getItem('userId') || '',
    token: localStorage.getItem('token') || '',
    userList: JSON.parse(localStorage.getItem('userList')) || {},
    selectUserInfo: JSON.parse(localStorage.getItem('selectUserInfo')) || {},
  }
}
const state = userInfoObj()

const mutations = {
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
  },
  SET_USER_ID: (state, userId) => {
    state.userId = userId
    sessionStorage.setItem('userId', userId)
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    localStorage.setItem('token', token)
  },
  SET_USER_LIST: (state, userList) => {
    state.userList = userList
    localStorage.setItem('userList', JSON.stringify(userList))
  },
  SET_SELECT_USER_INFO: (state, selectUserInfo) => {
    state.selectUserInfo = selectUserInfo
    localStorage.setItem('selectUserInfo', JSON.stringify(selectUserInfo))
  },
}

const actions = {
  setUserInfo({ commit }, userInfo) {
    commit('SET_USER_INFO', userInfo)
  },
  setUserId({ commit }, userId) {
    commit('SET_USER_ID', userId)
  },
  setToken({ commit }, token) {
    commit('SET_TOKEN', token)
  },
  setUserList({ commit }, userList) {
    commit('SET_USER_LIST', userList)
  },
  setSelectUserInfo({ commit }, selectUserInfo) {
    commit('SET_SELECT_USER_INFO', selectUserInfo)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
